package com.mdb.open.trade.service.trade;


/*
* Copyright (C), 2017-2019 澳发科技(MDB)
* FileName: TfLoanProcessLogService.java
* Author:   you.yichao
* Date:     2019-07-06
* History:
* <author>          <time>          <version>          <desc>
* you.yichao         2019-07-06          V1.00             代码生成器初始生成代码
* -----------------------------------------------------------
* 作者姓名           修改时间           版本号            描述
*/


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.mdb.bc.api.data.ListResultEx;
import com.mdb.bc.api.data.ResultEx;
import com.mdb.bc.database.mybatisplus.SuperService;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.open.common.enums.trade.type.FlowCusTypeEnum;
import com.mdb.open.common.enums.trade.type.FlowCusVisibleTypeEnum;
import com.mdb.open.trade.common.MdbDataSource;
import com.mdb.open.trade.domain.bo.trade.SaveLoanProcessLogBo;
import com.mdb.open.trade.domain.po.trade.LoanProcessLog;
import com.mdb.open.trade.mapper.trade.TfLoanProcessLogMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类描述：
 *  融资申请流程时序表 服务类
 *
 * @author you.yichao
 * @date 2019-07-06
 */
@Service
@DS(MdbDataSource.TRADE)
public class TfLoanProcessLogService extends SuperService<TfLoanProcessLogMapper, LoanProcessLog> {

    /**
     * 新增对应的时序记录
     */
    public ResultEx saveLoanProcessLog(SaveLoanProcessLogBo bo) {

        if (FlowCusVisibleTypeEnum.IS_VISIBLE.getValue().equals(bo.getType().getVisibleType().getValue())) {
            List<FlowCusTypeEnum> cusTypeEnumList = bo.getType().getCusTypeEnumList();
            int i = 0;
            for (FlowCusTypeEnum typeEnum : cusTypeEnumList) {
                LoanProcessLog addLoanProcessLog = new LoanProcessLog();
                addLoanProcessLog.setApplyOrderNo(bo.getApplyOrderNo());
                addLoanProcessLog.setCustomerCode(bo.getCustomerCode());
                addLoanProcessLog.setMerchantNo(bo.getMerchantNo());
                addLoanProcessLog.setFlowType(ObjectUtil.isEmpty(bo.getType().getProcessLogTypeEnumList())
                                                            ? bo.getType()
                                                            : bo.getType().getProcessLogTypeEnumList().get(i));
                addLoanProcessLog.setFinishDate(bo.getFinishDate());
                addLoanProcessLog.setCusFlowType(typeEnum);
                addLoanProcessLog.setCusVisibleType(FlowCusVisibleTypeEnum.IS_VISIBLE);
                if (this.baseMapper.insert(addLoanProcessLog) != 1) {
                    return ResultEx.fail();
                }
                i++;
            }
        } else {
            LoanProcessLog addLoanProcessLog = new LoanProcessLog();
            addLoanProcessLog.setApplyOrderNo(bo.getApplyOrderNo());
            addLoanProcessLog.setCustomerCode(bo.getCustomerCode());
            addLoanProcessLog.setMerchantNo(bo.getMerchantNo());
            addLoanProcessLog.setFlowType(bo.getType());
            addLoanProcessLog.setFinishDate(bo.getFinishDate());
            addLoanProcessLog.setCusVisibleType(FlowCusVisibleTypeEnum.UN_VISIBLE);
            if (this.baseMapper.insert(addLoanProcessLog) != 1) {
                return ResultEx.fail();
            }
        }

        return ResultEx.ok();
    }

    /**
     * 根据融资单编号查询时序列表（按创建时间倒叙）
     */
    public ListResultEx<LoanProcessLog> selectProcessLogList(String applyOrderNo){
        LambdaQueryWrapper<LoanProcessLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LoanProcessLog::getApplyOrderNo, applyOrderNo)
                             .eq(LoanProcessLog::isDeleted, Boolean.FALSE)
                             .orderByDesc(LoanProcessLog::getId);
        List<LoanProcessLog> list = this.baseMapper.selectList(queryWrapper);

        if(ObjectUtil.isEmpty(list)){
            return ListResultEx.ok(Lists.newArrayList());
        }

        return ListResultEx.ok(list);
    }
}
